/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-31
 * Time:11:53
 */
class MyArrayList<T>{//
    public T[] elem;
    public int usedSize;

    public MyArrayList(){
        this.elem = (T[])new Object[10];//不直接改为T类型的原因是:
    }

    public void add(T val){
        this.elem[usedSize] = val;
        this.usedSize ++;
    }

    public T get(int pos){
        return this.elem[pos];
    }
}
//写一个通用的顺序表

class MyArrayList2{
        public Object[] elem;
    public int usedSize;

    public MyArrayList2(){
        this.elem = new Object[10];
    }

    public void add(Object val){
        this.elem[usedSize] = val;
        this.usedSize ++;
    }

    public Object get(int pos){
        return this.elem[pos];
    }//什么都可以放了,但是希望是指定什么类型,就只能放什么类型
    //2.明知道是什么类型的元素但是还是要进行类型转换.为了解决这个问题提出了泛型....

}
    class MyArrayList1{
        public int[] elem;
        public int usedSize;

        public MyArrayList1(){
            this.elem = new int[10];
        }

        public void add(int val){
            this.elem[usedSize] = val;
            this.usedSize ++;
        }

        public int get(int pos){
            return this.elem[pos];
        }//这个顺序表只能放整型不通用

}

//泛型的意义:
//自动进行类型检查
//自动进行类型转换
//泛型类型的参数必须是类类型

class Person{

}
public class TestDemo {
    public static void main(String[] args) {
        Integer a = 10;//装箱
        //Integer b = new Integer(10);
        //System.out.println(a+ " " + b);

        int c = a;//拆箱
    }
    public static void main4(String[] args) {
        Person person = new Person();
        System.out.println(person);//类名+@+地址

        MyArrayList<String> myArrayList = new MyArrayList<>();
        System.out.println(myArrayList);
        MyArrayList<Integer> myArrayList1 = new MyArrayList<>();
        System.out.println(myArrayList1);
        MyArrayList<Person> myArrayList2 = new MyArrayList<>();
        System.out.println(myArrayList2);
    }
    public static void main3(String[] args) {
        MyArrayList<String> myArrayList = new MyArrayList<>();
        myArrayList.add("haha");//只能用字符串类型
//        myArrayList.add(1);//自动进行类型检查
        String s = myArrayList.get(0);//不需要进行强制类型转换
        System.out.println(s);
    }
    public static void main2(String[] args) {
        MyArrayList2 myArrayList = new MyArrayList2();
        myArrayList.add(1);
        myArrayList.add("haha");
        int val = (int) myArrayList.get(0);
        System.out.println(val);
    }
    public static void main1(String[] args) {
        MyArrayList1 myArrayList = new MyArrayList1();
        myArrayList.add(1);
        int val = myArrayList.get(0);
        System.out.println(val);
    }
}
